﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace WiMo.Games.WM5
{
    public class StateManager : IStateManager
    {
        IGameEngine _engine;

        public StateManager(IGameEngine engine)
        {
            _engine = engine;
        }

        private StateManager()
        {
        }

        private string Root
        {
            get
            {
                if(_engine.Game == null)
                    throw new NullReferenceException("You must initialize the Game property on the GameEngine before accessing storage values.");

                var assembly = _engine.Game.GetType().Assembly;
                return String.Format(@"WiMoGF/{0}/", assembly.FullName.Substring(0, assembly.FullName.IndexOf(',')));
            }
        }

        public void Store(string key, string value)
        {
            Microsoft.Win32.Registry.CurrentUser.SetValue(string.Format("{0}{1}", Root,key), value);
        }

        public string Get(string key, string defaultValue)
        {
            return Microsoft.Win32.Registry.CurrentUser.GetValue(string.Format("{0}{1}", Root,key), defaultValue).ToString();
        }

        public void ClearAll()
        {
            Microsoft.Win32.Registry.CurrentUser.DeleteSubKey(Root);
        }
    }
}
